package com.canieatit.gwt.server;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.canieatit.gwt.shared.Product;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;

public class DataStore {
	
	static {
		ObjectifyService.register(Product.class);
	}
	
	private static final Logger log = Logger.getLogger(DataStore.class.getName());

	static Objectify ofy;
	
	public static Product getFromDataStore(String barcode) {
		ofy = ObjectifyService.begin();
		log.log(Level.INFO, "Get product object from datastore for key: "+barcode);
		
		Product product;
		
		product = ofy.find(Product.class, barcode);
		
		return product;
	}
	
	public static boolean putToDataStore(Product product) {
		ofy = ObjectifyService.begin();
		
		log.log(Level.CONFIG, "Writing product to datastore with key: "+ product.productName);
		try {
			ofy.put(product);			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	public static void deleteFromDataStore(Product product) {
		ofy = ObjectifyService.begin();
		
		log.log(Level.INFO, "Deleting product from datastore with key : "+ product.productName);
		
		ofy.delete(product);
	}
}

